package icraf.carbon.gui.model;

import icraf.carbon.core.Zone.DataType;
import icraf.carbon.gui.MenuFactory.MenuProperty;
import icraf.carbon.model.ModelProject;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

public class ModelEmissionModPanel extends ModelIterationPanel {
	public class Listener implements ActionListener, FocusListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			if (e.getSource().equals(projectBG)) {
				updateBG();
			} else if (e.getSource().equals(factorUse)||e.getSource().equals(factorNot)) {
				updateEFactor();
			}
		}

		@Override
		public void focusGained(FocusEvent e) {
		}

		@Override
		public void focusLost(FocusEvent e) {
			if (e.getSource().equals(projectBG)) {
				updateBG();
			} else if (e.getSource().equals(factorUse)||e.getSource().equals(factorNot)) {
				updateEFactor();
			}
		}
	}

	private static final long serialVersionUID = 4305939712235366956L;
	public static final Color COLOR_E_FACTOR = new Color(150, 240, 90);
	
	private Listener listener = new Listener();
	private JCheckBox projectBG;
	private JRadioButton factorUse, factorNot;
	
	public ModelEmissionModPanel() {
		super();
		projectBG = new JCheckBox();
		projectBG.addActionListener(listener);
		projectBG.addFocusListener(listener);
		JPanel p = new JPanel(new FlowLayout(FlowLayout.LEFT));
		p.add(new JLabel(MenuProperty.INPUT_INCLUDE_BG.getLabel()+":"));
		p.add(projectBG);
		
		JPanel fPanel = new JPanel();
		factorUse = new JRadioButton(MenuProperty.INPUT_EMISSION_FACTOR.getLabel(), true);
		factorNot = new JRadioButton(MenuProperty.INPUT_ACTUAL_EMISSION.getLabel(), false);
		factorUse.addActionListener(listener);
		factorUse.addFocusListener(listener);
		factorNot.addActionListener(listener);
		factorNot.addFocusListener(listener);
		ButtonGroup bg = new ButtonGroup();
		bg.add(factorUse);
		bg.add(factorNot);
		fPanel.add(factorUse);
		fPanel.add(factorNot);
		factorUse.setBackground(COLOR_E_FACTOR);
		fPanel.setBackground(new Color(230, 217, 85));
		p.add(fPanel);
		setTopComponent(p);
	}

	
	public void setModelProject(ModelProject mp, DataType t, Object attributes) {
		if(mp.getSimulatedProject(0).isUsingModifiedFactor()) {
			t = DataType.MODIF_EMISSION_FACTOR;
		} else {
			t = DataType.MODIF_EMISSION;
		}
		super.setModelProject(mp, t, attributes);
//		setNegativeAllowed(true);
		projectBG.setSelected(mp.getSimulatedProject(0).isIncludeModifiedEmission());
		factorUse.setSelected(mp.getSimulatedProject(0).isUsingModifiedFactor());
		factorNot.setSelected(!mp.getSimulatedProject(0).isUsingModifiedFactor());
		
	}

	
	private void updateBG() {
		ModelProject m = getModelProject();
		if(m == null) return;
		for(int i = 0; i <= m.getIterationCount(); i++) {
			m.getSimulatedProject(i).setIncludeModifiedEmission(projectBG.isSelected());
		}
		m.updateSummary();
	}
	
	private void updateEFactor() {
		//getProject().setUsingBelowgroundFactor(factorUse.isSelected());
		ModelProject m = getModelProject();
		if(m == null) return;
		for(int i = 0; i <= m.getIterationCount(); i++) {
			m.getSimulatedProject(i).setUsingModifiedFactor(factorUse.isSelected());
		}
		//m.updateSummary();
		if(factorUse.isSelected()) {
			setDataType(DataType.MODIF_EMISSION_FACTOR,	null);
		} else {
			setDataType(DataType.MODIF_EMISSION,	null);
		}
	}


	
	
}
